/**
 * @file LayerManagerMode.cpp
 *
 * @date 30/12/2011
 * @author Tales 
 *
 */

#include "LayerManagerMode.hpp"
#include "../controller/Editor.hpp"
#include <sstream>

namespace yep
{

static const int STATE_PRESENTATION = 0, STATE_OPT_CREATE = 1, STATE_OPT_DESTROY = 2, STATE_OPT_CHANGE_POS = 3,
		STATE_OPT_MERGE = 4, STATE_ASK = 5, STATE_PARAM = 6, STATE_END = -1;

pair<string, ConsoleModeBase::Function> yep::LayerManagerMode::getNextLine(Editor & editor)
{
	string text;
	Function function;
	switch (state)
	{
	case STATE_PRESENTATION:
	{
		stringstream s;
		s << "Camada atual " << (editor.getCurrentLayer()+1) << "/" << editor.getLayerCount() << ". Escolha opção:";
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_OPT_CREATE;
		break;
	}
	case STATE_OPT_CREATE:
		text = "1 - Criar camada";
		function = NO_FUNCTION;
		state = STATE_OPT_DESTROY;
		break;
	case STATE_OPT_DESTROY:
		text = "2 - Destruir camada";
		function = NO_FUNCTION;
		state = STATE_OPT_CHANGE_POS;
		break;
	case STATE_OPT_CHANGE_POS:
		text = "3 - Mudar Posição";
		function = NO_FUNCTION;
		state = STATE_OPT_MERGE;
		break;
	case STATE_OPT_MERGE:
		text = "4 - Mesclar camada";
		function = NO_FUNCTION;
		state = STATE_ASK;
		break;
	case STATE_ASK:
	{
		stringstream s;
		s << "Qual opção deseja selecionar? (1 - 4 = mudar; 0 = nao mudar): ";
		function = INPUT_CHAR;
		text = s.str();
		break;
	}
	case STATE_PARAM:
	{
		string paramText[4] = { "Qual a posição (0 Cancela): ", "Qual posição deseja deletar (0 = Cancela): ",
				"A camada atual deve ir no lugar de trocada com qual (0 Cancela): ",
				"Deseja realmente mesclar (S = sim mas deixa as anteriores; D = sim e apaga as anteriores; N = não) :" };
		text = paramText[option];
		Function paramFunction[4] =
		{ INPUT_INT, INPUT_INT, INPUT_INT, INPUT_CHAR };
		function = paramFunction[option];
		break;
	}
	case STATE_END:
		text = "";
		function = END;
		break;
	}

	return make_pair(text, function);
}

void yep::LayerManagerMode::handleAnswer(Editor & editor, string answer)
{
	if (state == STATE_ASK)
	{
		stringstream s(answer);
		s >> option;
		if (option == 0)
			state = STATE_END;
		else if (option > 0 && option <= 4)
		{
			--option;
			state = STATE_PARAM;
		}
	}
	else if (state == STATE_PARAM)
	{
		stringstream s(answer);
		switch (option)
		{
		case 0:// Create
		{
			size_t iParam;
			s >> iParam;
			if(iParam <= editor.getLayerCount()+1)
			{
				if(iParam > 0)
					editor.addLayer(iParam-1);
				state = STATE_END;
			}
			break;
		}
		case 1: //Destroy
		{
			size_t iParam;
			s >> iParam;
			if (iParam <= editor.getLayerCount())
			{
				if (iParam > 0)
					editor.removeLayer(iParam - 1);
				state = STATE_END;
			}
			break;
		}
		case 2: // ChangePos
		{
			size_t iParam;
			s >> iParam;
			if (iParam <= editor.getLayerCount())
			{
				if (iParam > 0)
					editor.changePosLayer(iParam - 1);
				state = STATE_END;
			}
			break;
		}
		case 3: // Merge
		{
			char iParam;
			s >> iParam;
			if ('S' == toupper(iParam))
			{
				editor.merge(false);
				state = STATE_END;
			}
			else if ('D' == toupper(iParam))
			{
				editor.merge(true);
				state = STATE_END;
			}
			else if ('N' == toupper(iParam))
				state = STATE_END;
			break;
		}
		default:
			break;
		}
	}
}

void yep::LayerManagerMode::setup()
{
	ConsoleModeBase::setup();
	state = STATE_PRESENTATION;
}
}

/* namespace yep */
